package emulator.hardware.io.peripherals.iec;

import java.util.HashMap;

public class DriveStatus {
	public static final int OK = 0;
	public static final int SCRATCH_RESULT = 1;
	public static final int FILE_NOT_OPEN = 61;
	public static final int FILE_NOT_FOUND = 62;
	public static final int DISK_FULL = 72;
	public static final int RESET = 73;
	public static final int NO_DISK = 74;
	
	private HashMap<Integer, String> text_map = new HashMap<Integer, String>();
	private int status = 0;
	private int val1 = 0;
	private int val2 = 0;

	public DriveStatus(String dos_version)
	{
		text_map.put(OK, " OK");
		text_map.put(SCRATCH_RESULT, "FILES SCRATCHED");
		text_map.put(FILE_NOT_OPEN, "FILE NOT OPEN");
		text_map.put(FILE_NOT_FOUND, "FILE NOT FOUND");
		text_map.put(DISK_FULL, "DISK FULL");
		text_map.put(NO_DISK, "DRIVE NOT READY");
		text_map.put(RESET, dos_version);
	}
	
	public void setStatus(int status, int val1, int val2)
	{
		this.status = status;
	}
	
	public boolean hasError()
	{
		return status > 1;
	}
	
	public void clearStatus()
	{
		status = OK;
		val1 = 0;
		val2 = 0;
	}
	
	public int getStatus()
	{
		return status;
	}
	
	String readStatus()
	{
		String result = getNumString(status) + ",";
		if (text_map.containsKey(status))
			result += text_map.get(status);
		result += ","+getNumString(val1)+","+getNumString(val2)+"\r";
		return result;
	}

	private String getNumString(int number) {
		return (number < 10 ? "0" : "") + number;
	}
}
